<?php
// +----------------------------------------------------------------------
// | Buddy Framework 
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://buddy.woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <xinqiyang@gmail.com>
// +----------------------------------------------------------------------

/**
 * Time functions
 * 
 * @author xinqiyang
 *
 */
class Time {
	
	/**
	 * show friend time
	 * @param $datetime time
	 * @return string friend time show
	 */
	public static function timeAgo($datetime, $nowtime = 0)
	{
		$datetime = $datetime>0 ? $datetime : strtotime($datetime);
		if (empty($nowtime))
		{
			$nowtime = time();
		}
		$timediff = $nowtime - $datetime;
		$timediff = $timediff >= 0 ? $timediff : $datetime - $nowtime;
		// 秒
		if ($timediff < 60)
		{
			return $timediff . '秒前';
		}
		// 分
		if ($timediff < 3600 && $timediff >= 60)
		{
			return intval($timediff / 60) . '分钟前';
		}
		// 今天
		$today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
		if ($datetime >= $today)
		{
			return date('今天 H:i', $datetime);
		}
		// 昨天
		$yestoday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
		if ($datetime >= $yestoday)
		{
			return date('昨天 H:i', $datetime);
		}
		// 今年月份
		$this_year = mktime(0, 0, 0, 1, 1, date('Y'));
		if ($datetime >= $this_year)
		{
			return date('m月d日 H:i', $datetime);
		}
		// 往年
		return date('Y年m月d日', $datetime);
	}
	
	
	public static function tranTime($time) {  
	    $rtime = date("m-d H:i",$time);  
	    $htime = date("H:i",$time);  
	      
	    $time = time() - $time;  
	  
	    if ($time < 60) {  
	        $str = '刚刚';  
	    }  
	    elseif ($time < 60 * 60) {  
	        $min = floor($time/60);  
	        $str = $min.'分钟前';  
	    }  
	    elseif ($time < 60 * 60 * 24) {  
	        $h = floor($time/(60*60));  
	        $str = $h.'小时前 '.$htime;  
	    }  
	    elseif ($time < 60 * 60 * 24 * 3) {  
	        $d = floor($time/(60*60*24));  
	        if($d==1)  
	           $str = '昨天 '.$rtime;  
	        else  
	           $str = '前天 '.$rtime;  
	    }  
	    else {  
	        $str = $rtime;  
	    }  
	    return $str;  
	}
	

    /**
     * yesterday time
     * @return int timestamp
     */
    public static function yesterday() {
        return strtotime('-1 day');
    }

    /**
     * today first timestamp
     * @return int timestamp
     */
    public static function today() {
        return strtotime('today');
    }

    //TODO: NEED DO
    public static function lastMonth() {
        
    }

    //TODO: NEED DO
    public static function thisQuarter($format)
    {
        $begin = mktime(0,0,0,date('n')-(date('n')-1)%3,1,date('Y'));
    }

    /**
     * return this Month date array
     * @return array
     */
    public static function thisMonth($format = 'Ymd')
    {
        return self::fromtoTime(mktime(0,0,0,date('n'),1,date('Y')), mktime(0,0,0,date('n'),date('t'),date('Y')),$format);
    }

    /**
     * lastWeek date arr
     * @return array
     */
    public static function lastWeek($format='Ymd') {
        return self::natureWeek(strtotime("-1 week Sunday"), $format);
    }

    /**
     * this week date arr
     * @param string $format int/timeformat
     * @return array
     */
    public static function thisWeek($format='Ymd') {
        return self::natureWeek('', $format);
    }

    /**
     * get natureweek first and end
     * @param mixed $date null/string/int
     * @param string $format int/time format
     * @return array
     */
    public static function natureWeek($date='',$format='Ymd') {
        $timestamp = empty($date) ? strtotime('today') : (is_int($date) ? $date : strtotime($date));
        $sdate = $timestamp - (date('N', $timestamp) - 1) * 86400;
        $edate = $timestamp + (7 - date('N', $timestamp)) * 86400;
        return self::fromtoTime($sdate, $edate,$format);
    }


    /**
     * from begin to end time
     * @param mixed $begin  timestamp/string
     * @param mixed $end   timestamp/string
     * @param string $format int/time format, Ymd/Y-m-d/Y-m-d 00:00:00
     * @return mixed array/false
     */
    public static function fromtoTime($begin, $end, $format='Ymd') {
        if ($begin && $end) {
            $begin = is_int($begin) ? $begin : strtotime($begin);
            $end = is_int($end) ? $end : strtotime($end);
            $arr = array();
            if ($end > $begin) {
                $days = ceil(($end - $begin) / (24 * 3600));
                if ($days) {
                    for ($i = $days; $i >= 0; $i--) {
                        if($format === 'int')
                        {
                            array_push($arr, $begin + 24 * $i * 3600);
                        }else{
                            array_push($arr, date($format, $begin + 24 * $i * 3600));
                        }
                    }
                }
            }
            return $arr;
        }
        return false;
    }
}